package first.endtoend.tsm;

import org.apache.log4j.Logger;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.elements.OperationResult;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.*;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.responses.BasicResponseType;

import java.io.IOException;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by ncordina on 15/06/2015.
 * This Class provide easy initialisating class for TSMHelper
 * Instaead of writing same lines of codes, you can use thoose functions for initialisating TSM webservice methods more
 * quickly
 */
public class TSMHelperTools
{

    protected static Logger logger = Logger.getLogger(TSMHelper.class.getName());
    private static String majorVersion = "1";
    private static String revisionVersion = "0";
    private static String minorVersion = "0";
    private String mnoName = "ORANGE UK MNO";
    private String ICCID = "89354010090401005388";
    private String serviceQualifierName = "1";
    private String MSISDN = "3548582394";

    /**
     * Empty Constructor
     */
    public TSMHelperTools()
    {
    }



    /**
     * This method is a simple tool for converting a HashMap into string
     *
     * @param hashMap The HashMap to convert
     * @return The converter HashMap
     */
    public static String hashMapDisplay(HashMap<String, String> hashMap)
    {
        Iterator it = hashMap.entrySet().iterator(); //Get the Iterator
        String str = ""; //Initialize the string to return
        while (it.hasNext()) //While there is a next element
        {
            Map.Entry couple = (Map.Entry) it.next(); //The actual Key Value couple
            str += "* " + couple.getKey() + " : " + couple.getValue() + "\n"; //Format the string with the Key and Value
            it.remove(); // avoids a ConcurrentModificationException
        }
        return str;
    }

    /**
     * Method use for displaying basic response from TSM webservices
     *
     * @param operationResult Operation Resulto to display
     * @param gpFunction      Function to check
     * @return the Hashmap to display
     */
    public static HashMap<String, String> handleResponse(OperationResult operationResult, String gpFunction)
    {
        BasicResponseType responseType = (BasicResponseType) operationResult.Result;
        HashMap<String, String> display = new HashMap<String, String>();
        if (operationResult.MethodName.equalsIgnoreCase(gpFunction))
        {
            try
            {
                if(responseType != null)
                {
                    if (responseType.ProcessingStart != null)
                    {
                        display.put("Processing Start", responseType.ProcessingStart.toString());
                    }
                    if (responseType.ProcessingEnd != null)
                    {
                        display.put("Processing End", responseType.ProcessingEnd.toString());
                    }
                    if (responseType.AcceptableValidityPeriod != null)
                    {
                        display.put("Acceptable Validity Period", responseType.AcceptableValidityPeriod.toString());
                    }
                    if (responseType.FunctionExecutionStatus != null)
                    {
                        ExecutionStatusType executionStatus = responseType.FunctionExecutionStatus;
                        display.put("Execution status", executionStatus.Status.toString());
                        display.put("Execution Subject", executionStatus.StatusCodeData.Subject.toString());
                        display.put("Execution Reason", executionStatus.StatusCodeData.Reason.toString());
                        display.put("Execution Message", executionStatus.StatusCodeData.Message.toString());
                        display.put("Execution Subject Identifier", executionStatus.StatusCodeData.SubjectIdentifier.toString());
                    }
                }else
                {
                    display.put("FALSE","RESPONSE TYPE IS EMPTY");
                }

            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return display;
    }

    /**
     * Init a service Identifier with test Datas
     * MajorVersion = 52
     * MinorVersion = 1
     * REvisionVersion = 2
     * ServiceID = 00001
     *
     * @return the Service Identifier
     */
    public ServiceIdentifierType initService()
    {
        ServiceIdentifierType service = new ServiceIdentifierType();
        service.ServiceId = BigInteger.valueOf(2); //Random ServiceID
        service.ServiceVersion = new ServiceVersionType();
        service.ServiceVersion.MajorVersion = majorVersion; //Random MajorVersion
        service.ServiceVersion.MinorVersion = minorVersion; //Random MinorVersion
        service.ServiceVersion.RevisionVersion = revisionVersion; //Random Revision Version

        return service;
    }

    /**
     * Init Service object with a Service ID Parameter
     *
     * @param serviceID
     * @return
     */
    public ServiceIdentifierType initService(String serviceID)
    {
        ServiceIdentifierType service = new ServiceIdentifierType();
        service.ServiceId = new BigInteger(serviceID);
        service.ServiceVersion = new ServiceVersionType();
        service.ServiceVersion.MajorVersion = majorVersion; //Random MajorVersion
        service.ServiceVersion.MinorVersion = minorVersion; //Random MinorVersion
        service.ServiceVersion.RevisionVersion = revisionVersion; //Random Revision Version
        return service;
    }


    public MSId_MSISDNType initMobileSubcription()
    {
        MSId_MSISDNType mobile = new MSId_MSISDNType();
        mobile.MNOName = this.mnoName;
        mobile.MSISDN = this.MSISDN;
        return mobile;
    }

    /**
     * Init a Mobile Subscription Identiier Type with test Data
     *
     * @param mnoName mnoName param
     * @return The Mobile Subscription Identifier
     */
    public MSId_MSISDNType initMobileSubcription(String mnoName)
    {
        MSId_MSISDNType mobile = new MSId_MSISDNType();
        mobile.MNOName = mnoName;
        mobile.MSISDN = this.MSISDN;

        return mobile;
    }

    /**
     * Init the Secure Element Type with test Data
     * The SE ID is 0000000
     *
     * @return the Secure element Type
     */
    public SEId_ICCIDType initSecureElement()
    {
        SEId_ICCIDType seid = new SEId_ICCIDType();
        seid.ICCID = this.ICCID;

        return seid;
    }

    public SEId_ICCIDType initSecureElement(String ICCID)
    {
        SEId_ICCIDType seid = new SEId_ICCIDType();
        seid.ICCID = ICCID;

        return seid;
    }

    /**
     * Init Service Qualifier Type with test Data
     * The Qualifier is "QUALIFIER"
     *
     * @return the Service Qualifier Type
     */
    public ServiceQualifierType initServiceQualifier()
    {
        ServiceQualifierType serviceQualifier = new ServiceQualifierType();
        serviceQualifier.Qualifier = this.serviceQualifierName;

        return serviceQualifier;
    }

    /**
     * Init Service Qualifier Type
     * The Qualifier is "QUALIFIER"
     *
     * @return the Service Qualifier Type
     */
    public ServiceQualifierType initServiceQualifier(String qualifier)
    {
        ServiceQualifierType serviceQualifier = new ServiceQualifierType();
        serviceQualifier.Qualifier = qualifier;

        return serviceQualifier;
    }


    public ServiceInstanceReferenceType initServiceReferenceType()
    {
        ServiceInstanceReferenceType serviceInstance = new ServiceInstanceReferenceType();
        return serviceInstance;
    }

    /**
     * This is a tool for converting a ServiceInstanceState code into a string
     *
     * @param bigInteger The bigInteger return by the ServiceInstanceState
     * @return The string with the code and the associated value
     */
    public String getStringFromServiceInstanceState(BigInteger bigInteger)
    {
        String str = "";
        BigInteger one = new BigInteger("1");
        BigInteger eleven = new BigInteger("11");
        BigInteger twelve = new BigInteger("12");
        BigInteger thirteen = new BigInteger("13");
        BigInteger twentyone = new BigInteger("21");
        BigInteger twentytwo = new BigInteger("22");
        BigInteger twentythree = new BigInteger("23");

        if (bigInteger.equals(one))
        {
            str = "1 => Not Deployed";
        } else if (bigInteger.equals(eleven))
        {
            str = "11 => Installed";
        } else if (bigInteger.equals(twelve))
        {
            str = "12 => Personalized";
        } else if (bigInteger.equals(thirteen))
        {
            str = "13 => Incomplete Deployment";
        } else if (bigInteger.equals(twentyone))
        {
            str = "21 => Active";
        } else if (bigInteger.equals(twentytwo))
        {
            str = "22 => Suspended";
        } else if (bigInteger.equals(twentythree))
        {
            str = "23 => Missing Mobile Environment Component";
        } else
        {
            str = bigInteger.toString() + " is not valid or is not a recognized code";
        }

        return str;
    }

    public static HashMap<String,String> FillTestMatrix()
    {
        HashMap<String,String> display = new HashMap<String,String>();
        display.put("ProcessingStart",new Date().toString());
        display.put("ProcessingEnd",new Date().toString());
        display.put("AcceptableValidityPeriod","60000");
        display.put("Status", "EXECUTED_SUCCESS");
        display.put("Subject","Test CheckGlobalEligibility service");
        display.put("Reason","0");
        display.put("SubjectIdentifier","CheckGlobalEligibility");
        display.put("Message","Call service sucessful");
        display.put("Eligibile","True");

        return display;

    }




}
